package com.shumei.service.impl;

import com.alibaba.fastjson.JSON;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.shumei.dao.mapper.UInformationMapper;
import com.shumei.dao.mapper.ULoginMapper;

import com.shumei.dao.pojo.UInformation;
import com.shumei.dao.pojo.ULogin;
import com.shumei.service.LoginService;
import com.shumei.service.UInformationService;
import com.shumei.service.ULoginService;
import com.shumei.utils.JWTUtils;
import com.shumei.vo.ErrorCode;
import com.shumei.vo.Result;
import com.shumei.vo.params.RegisterParam;
import com.shumei.vo.params.ULoginParam;
import org.apache.commons.codec.digest.DigestUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Service
public class ULoginServiceImpl implements ULoginService {
    //加密盐
    private static final String slat="shumei!@#";
    @Autowired
    ULoginMapper uLoginMapper;
    @Autowired
    UInformationService uInformationService;
    @Autowired
    UInformationMapper uInformationMapper;
    @Override
    public Long getid(String accountnum, String password){
        Long uLoginid = uLoginMapper.getULoginid(accountnum, password);
        return uLoginid;

    }
//    @Override
//    public void regist(String accountnum, String password,UInformation uInformation){
//        Long id=getid(accountnum,password);
//        System.out.println(id);
//        if (id!=null) {
//            System.out.println("该用户已存在");
//        }
//        else{
//            ULogin uLogin = new ULogin();
//            uLogin.setAccountnum(accountnum);
//            uLogin.setPassword(password);
//            uLogin.setCreateTime(new Date());
//            int result= uLoginMapper.insert(uLogin);
//            Long id2=getid(accountnum,password);
//            if (result>0){
//                System.out.println("注册成功");
//                uInformation.setUId(id2);
//                uInformation.setCreateTime(new Date());
//                uInformationService.registsetdata(uInformation);
//            }else {
//                System.out.println("注册失败");
//            }
//
//        }
//
//
//    }
//
//    @Override
//    public UInformation login(String accountnum, String password){
//        Long uLoginid = uLoginMapper.getULoginid(accountnum, password);
//        System.out.println(uLoginid);
//        UInformation userdata=uInformationService.getdata(uLoginid);
//        return userdata;
//
//    }
    @Autowired
    LoginService loginService;

    @Autowired
    RedisTemplate redisTemplate;
    @Override
    public Result regist(RegisterParam registerParam) {

        /**
         * 1、判断参数是否合法
         * 2、判断用户是否存在，存在返回被注册
         * 3、不存在注册用户
         * 4、生成token
         * 5、存入redis，并返回
         * 6、注意加上事务，中间出现任何问题，需要回滚
         */
        String username = registerParam.getUsername();
        String password = registerParam.getPassword();


        if (StringUtils.isBlank(username)
                || StringUtils.isBlank(password)

        ){
            return Result.fail(ErrorCode.PARAMS_ERROR.getCode(),ErrorCode.PARAMS_ERROR.getMsg());
        }
        ULogin uLogin = this.loginService.findULoginByName(username);

        if (uLogin != null){
            return Result.fail(ErrorCode.ACCOUNT_EXIST.getCode(),ErrorCode.ACCOUNT_EXIST.getMsg());
        }
        uLogin = new ULogin();

        uLogin.setAccountnum(username);
        uLogin.setPassword(DigestUtils.md5Hex(password+slat));
        uLogin.setCreateTime(new Date());
        uLogin.setUpdateTime(new Date());
        this.uLoginMapper.insert(uLogin);

        UInformation uInformation = new UInformation();
        uInformation.setUId(uLogin.getUId());
        uInformation.setUName(registerParam.getUName());
        uInformation.setSex(registerParam.getSex());
        uInformation.setAddress(registerParam.getAddress());
        uInformation.setPhone(registerParam.getPhone());
        uInformation.setEmail(registerParam.getEmail());
        uInformation.setQq(registerParam.getQq());
        uInformation.setIdentity(registerParam.getIdentity());
        uInformation.setBirthday(registerParam.getBirthday());
        uInformation.setCreateTime(new Date());
        uInformation.setUpdateTime(new Date());
        uInformation.setPicture(registerParam.getPicture());
        this.uInformationMapper.insert(uInformation);

        String token = JWTUtils.createToken(uLogin.getUId());

        redisTemplate.opsForValue().set("TOKEN_"+token, JSON.toJSONString(uLogin),1, TimeUnit.DAYS);
        return Result.success(token);
    }

}
